/*
			Author: Ozcan Degirmenci (Boot)
			E-Mail: odegirmenci@hotmail.com
			Date:	4 Jan 2005
*/

using System;
using System.Collections;
using System.Drawing;

namespace Ling.ZhControls
{
	/// <summary>
	///  SimpleTabPageCollection
	/// </summary>
	public class SimpleTabPageCollection : CollectionBase
	{
		SimpleTabControl	_Parent				= null				;
		bool				_Calculated			= false				;
		bool				suspendDraw			= false				;
		

		internal int LastCalculated = 0;

		/// <summary>
		///  Initialize a new instance fo this class
		/// </summary>
		internal SimpleTabPageCollection(SimpleTabControl parent) : base ()
		{
			_Parent = parent;
		}

		public void AddRange(SimpleTabPage[] items)
		{
			suspendDraw = true;
			for (int i = 0; i < items.Length; i++)
			{
				Add(items[i]);
			}
			suspendDraw = false;

			Parent.DrawControl ();
		}

		protected override void OnRemoveComplete(int index, object value)
		{
			base.OnRemoveComplete(index, value);
			SimpleTabPage item = ((SimpleTabPage) value);
			item.Parent = null;
			if (Parent.SelectedItem == item)
			{
				if (Parent.Tabs.Count > 0)
				{
					index--;
					if (index < 0)
						index = 0;
					Parent.SetInnerSelected (index);
				}
			}
			if (!suspendDraw)
			{
				Parent.Invalidate ();
			}
		}

		public int Add(SimpleTabPage item)
		{
			int index;
			index = base.Count;
            item.ParentCollection = this;
			item.Visible = false;
			if (Parent != null)
			{
				item.Parent = Parent.ItemsParent; // Parent
				if (Parent.DrawMode == TabDrawMode.Gradient)
					item.BackColor = Color.Transparent;
				else
					item.BackColor = Parent.SelectedTabColor;
			}
			this.Insert(index, item);
			if (Count > 0 && Parent.SelectedItem == null)
				Parent.SelectedIndex = index;
			return index; 
		}

		public void Insert(int index, SimpleTabPage item)
		{
			base.List.Insert(index, item);
		} 

		public void Remove(SimpleTabPage item)
		{
			base.List.Remove(item);
		}

		public bool Contains(SimpleTabPage item)
		{
			return base.List.Contains(item); 
		}

		public int IndexOf(SimpleTabPage item)
		{
			return base.List.IndexOf(item); 
		}

		public void CopyTo(SimpleTabPage[] array, int index)
		{
			base.List.CopyTo(array, index);
		}

		public SimpleTabPage this[int index]
		{
			get
			{
				return ((SimpleTabPage) base.List[index]); 
			}
		}

		public SimpleTabPage ItemFromPoint (Point pt)
		{
			if (Parent == null)
				return null;
			if (Parent.PanelArea.Contains(pt))
			{
				if (Parent.SelectedItem == null)
					if (Count > 0)
						return this[0];
					else
						return null;
				else
					return Parent.SelectedItem;
			}

			for (int i = 0; i < Count; i++)
				if (this[i].TabRectangle.Contains (pt))
					return this[i];
			return null;
		}

		internal void Calculate (Graphics g)
		{
			int x = Parent.TabArea.X + 3; 
			int y = Parent.TabArea.Y;
			if (Parent.TabsLayout == TabsLayout.Bottom)
				y = Parent.PanelArea.Bottom;
			LastCalculated = 0;

			for (int i = 0; i < Count; i++)
			{
				if (i < Parent.FirstVisible || !this[i].IsNeedToDraw)// | !this[i].Visible)
				{
					this[i].TabRectangle = Rectangle.Empty;
					continue;
				}
				
				int width = 0;

				if (Parent.FixTabWidth == 0)
				{
					SizeF sf;
					// UPDATE:On 31/01/2005 By Ozcan Degirmenci
					// We started to use always bold font for calculation.
					// In this way our Tabs always have same width
					// even they are selected or not ...
					//if (Parent.SelectedIndex == i)
					//{
						sf = g.MeasureString(this[i].Text, Parent.selectedFont);
					//}
					//else
					//{
					//	sf = g.MeasureString(this[i].Text, Parent.Font);
					//}

					int tempWidth = (int)Math.Ceiling((double)sf.Width) + 20 + 10;// 10 additional width for the text
					//  because width wont seen always correctly also we use Math.Ceeling
					//tempWidth += 30;

					if (this[i].ImageIndex != -1)
						tempWidth += 20;
					//if(width < tempWidth)
					width = tempWidth;
				}
				else
				{
					width = Parent.FixTabWidth;
				}

				if (width + x > Parent.OnlyTabsArea.Right)
				{
					this[i].TabRectangle = Rectangle.Empty;
				}
				else
				{
					LastCalculated = i;
					this[i].TabRectangle = new Rectangle(x, y, 
						width, Parent.TabHeight);
				}

				x += width - 15;
			}

			// Force always calculating ...
			// Making this true after calculating need to make
			// false this property every time we need to calculate ...
			Calculated = false;
		}

		internal SimpleTabControl Parent
		{
			get {return _Parent;}
		}

		internal bool Calculated
		{
			get {return _Calculated;}
			set {_Calculated = value;}
		}
	}
}
